﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qfile.h>
#include <qgridlayout.h>
#include <QDir>
#include <QDebug>

#if _MSC_VER >= 1600 //vs2010 sp1

#pragma warning (disable:4819)//文件格式
#pragma warning (disable:4100)//未引用形参
#pragma warning(disable:4068) //清除警告
#pragma execution_character_set("utf-8")

#endif

MainWindow::MainWindow(QString sComNum,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_QuitOnClose,true);

    this->setStyleSheet("QWidget{font-family: 'Microsoft YaHei';}");


    _pConfigProcess = new QConfigProcess(this);
    _pConfigProcess->getDevConfig();

    _pCapControl = CCapControl::getInstance();
    bool bRel =_pCapControl->startRcvData(sComNum,230400);
    if(bRel == false)
    {
        QMessageBox::warning(this,"提示","打开串口失败！");
        exit(0);
        return;
    }


    InitUI();


    connect(_pCapControl,&CCapControl::signal_showloopdata,this,&MainWindow::onRcvData);

    //参数设置
    connect(_pSetParamWidget,&QSetParamWidget::signal_set_thresholdparam,this,&MainWindow::onSetThresholdParam);
    connect(_pSetParamWidget,&QSetParamWidget::signal_get_thresholdparam,this,&MainWindow::onGetThresholdParam);
    connect(_pCapControl,&CCapControl::signal_rcvthresholddata,_pSetParamWidget,&QSetParamWidget::onRcvData);

    //电流环
    connect(_pCurrentTestWidget,&QCurrentTestWidget::signal_getpid,_pCapControl,&CCapControl::getAxisParam);
    connect(_pCurrentTestWidget,&QCurrentTestWidget::signal_setpid,_pCapControl,&CCapControl::setAxisParam);
    connect(_pCapControl,&CCapControl::signal_rcvpiddata,_pCurrentTestWidget,&QCurrentTestWidget::onRcvPidParam);
    connect(_pCurrentTestWidget,&QCurrentTestWidget::signal_startTest,this,&MainWindow::onGroundTest);


    //速度环
    connect(_pSpeedTestWidget,&QSpeedTestWidget::signal_getpid,_pCapControl,&CCapControl::getAxisParam);
    connect(_pSpeedTestWidget,&QSpeedTestWidget::signal_setpid,_pCapControl,&CCapControl::setAxisParam);
    connect(_pSpeedTestWidget,&QSpeedTestWidget::signal_startTest,this,&MainWindow::onGroundTest);
    connect(_pCapControl,&CCapControl::signal_rcvpiddata,_pSpeedTestWidget,&QSpeedTestWidget::onRcvPidParam);


    //位置环
    connect(_pPosTestWidget,&QPosTestWidget::signal_getpid,_pCapControl,&CCapControl::getAxisParam);
    connect(_pPosTestWidget,&QPosTestWidget::signal_setpid,_pCapControl,&CCapControl::setAxisParam);
    connect(_pPosTestWidget,&QPosTestWidget::signal_startTest,this,&MainWindow::onGroundTest);
    connect(_pCapControl,&CCapControl::signal_rcvpiddata,_pPosTestWidget,&QPosTestWidget::onRcvPidParam);




    //握手和解锁测试
//    connect(_pHandShakeAndUnlockWidget->_pUnlockwidget,&QUnLockWidget::signal_unlockcmd,this,&MainWindow::onSetUnlockCmd);
//    connect(_pHandShakeAndUnlockWidget->_pAziHandShakeWidget,&QHandShakeWidget::signal_sethandshake,this,&MainWindow::onSetAixsHandShakeParam);
//    connect(_pHandShakeAndUnlockWidget->_pPitchHandShakeWidget,&QHandShakeWidget::signal_sethandshake,this,&MainWindow::onSetAixsHandShakeParam);
//    connect(_pHandShakeAndUnlockWidget->_pAziHandShakeWidget,&QHandShakeWidget::signal_startTest,this,&MainWindow::onGroundTest);
//    connect(_pHandShakeAndUnlockWidget->_pPitchHandShakeWidget,&QHandShakeWidget::signal_startTest,this,&MainWindow::onGroundTest);

    //性能测试
//    connect(_pAziFunTestWidget,&QFunTestWidget::signal_startTest,this,&MainWindow::onGroundTest);
//    connect(_pPitchFunTestWidget,&QFunTestWidget::signal_startTest,this,&MainWindow::onGroundTest);
//    connect(_pAziFunTestWidget,&QFunTestWidget::signal_set_thresholdparam,this,&MainWindow::onSetThresholdParam);
//    connect(_pPitchFunTestWidget,&QFunTestWidget::signal_set_thresholdparam,this,&MainWindow::onSetThresholdParam);




//    //参数设置
//    connect(_pCurrentParamWidget,&QCurrentParamWidget::signal_startTest,this,&MainWindow::onGroundTest);
//    connect(_pCurrentParamWidget,&QCurrentParamWidget::signal_setpid,this,&MainWindow::onSetAxisParam);



}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::InitUI()
{
    _pSetParamWidget = new QSetParamWidget(this);

    _pCurrentTestWidget = new QCurrentTestWidget(this);

    _pSpeedTestWidget = new QSpeedTestWidget(this);

    _pPosTestWidget = new QPosTestWidget(this);

//    _pHandShakeAndUnlockWidget = new QHandShakeAndunLockWidget(this);

//    _pAziFunTestWidget = new QFunTestWidget(0x0,this);
//    _pPitchFunTestWidget = new QFunTestWidget(0x01,this);

    _pTopWidget = new QTopWidget("高性能伺服驱动调试软件","",this);//初始化顶部显示视图
    _pNavWidget = new QNavWidget(this);//导航栏  设备调试窗口

    //添加导航栏信息 以及对应的组件信息
    int nGroupID = _pNavWidget->addGroup("参数设置",":png/dev.png");//添加分组
    _pNavWidget->addItemWidget(nGroupID,"参数设置",_pSetParamWidget,":png/zero.png");//添加子项
    nGroupID = _pNavWidget->addGroup("参数测试",":png/dev.png");//添加分组
    _pNavWidget->addItemWidget(nGroupID,"电流环测试",_pCurrentTestWidget,":png/zero.png");//添加子项

    _pNavWidget->addItemWidget(nGroupID,"速度环测试",_pSpeedTestWidget,":png/zero.png");//添加子项

    _pNavWidget->addItemWidget(nGroupID,"位置环测试",_pPosTestWidget,":png/zero.png");//添加子项



    QWidget* pwidget = new QWidget(this);
    //设置背景白色
    QPalette pal(pwidget->palette());
    pal.setColor(QPalette::Background,QColor(255,255,255));
    pwidget->setAutoFillBackground(true);
    pwidget->setPalette(pal);

    QGridLayout* playout = new QGridLayout(pwidget);
    playout->setMargin(0);
    playout->setSpacing(0);
    playout->addWidget(_pTopWidget,0,0,1,1);
    playout->addWidget(_pNavWidget,1,0,1,1);

    playout->setColumnStretch(0,1);
    playout->setRowStretch(1,1);
    playout->setRowStretch(0,1);
    playout->setRowStretch(0,20);

    this->setCentralWidget(pwidget);

    this->setWindowFlags(Qt::Window|Qt::FramelessWindowHint |Qt::WindowSystemMenuHint|Qt::WindowMinimizeButtonHint|Qt::WindowMaximizeButtonHint);//无边框
    this->showMaximized();

}

/**
* @brief
*
**/
void MainWindow::onRcvData(QVariant var)
{
    st_loopbackdata l_rcvdata = var.value<st_loopbackdata>();
    _pCurrentTestWidget->rcvFrameData(l_rcvdata);
    _pPosTestWidget->rcvFrameData(l_rcvdata);
    _pSpeedTestWidget->rcvFrameData(l_rcvdata);

}

/**
* @brief 速度给定模式或位置给定模式参数设置
* @note
*
*/
void MainWindow::onGetThresholdParam(int i32AxisNo,int i32Cmdid)
{
    bool bRel = _pCapControl->getThresholdParam(i32AxisNo,i32Cmdid);
    if(bRel == false)
    {
        QMessageBox::warning(this,"提示","获取参数失败!");
    }
    else
    {
      //  QMessageBox::warning(this,"提示","获取参数成功!");
    }
}
/**
* @brief 速度给定模式或位置给定模式参数设置
* @note
*
*/
void MainWindow::onSetThresholdParam(int i32AxisNo,int i32Cmdid,int i32Value,int datalen)
{
    bool bRel = _pCapControl->setThresholdParam(i32AxisNo,i32Cmdid,i32Value,datalen);
    if(bRel == false)
    {
        QMessageBox::warning(this,"提示","设置参数失败!");
    }
}


/**
* @brief
* @note
*
*/
void MainWindow::onGroundTest(int i32Axis,int workmode, int data0,int data1)
{
    _pCapControl->startGroudTest(i32Axis,workmode,data0,data1);
}
/**
* @brief
* @note
*
*/
void MainWindow::onSetUnlockCmd(int cmd,int value)
{
    _pCapControl->setUnlockCmd(cmd,value);
    qDebug()<<"解锁指令:"<<cmd;
}
/**
* @brief
* @note
*
*/
void MainWindow::onSetAixsHandShakeParam(int i32AxisNo,int i32value,int datalen)
{
    _pCapControl->setAxisHandShakeParam(i32AxisNo,i32value,datalen);
    qDebug()<<"握手信号:"<<i32AxisNo<<i32value;
}
